package scut.facerecognition.mapper;
import org.apache.ibatis.annotations.*;
import scut.facerecognition.entity.pojo.Message;

import java.util.List;

@Mapper
public interface MessageMapper  {
    @Insert("insert into message (sender_id, receiver_id, content, create_time, is_delivered, message_status) " +
            "VALUES (#{senderId},#{receiverId},#{content},#{createTime},#{isDelivered},#{messageStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "messageId")//获取生成的主键id的值
    void insert(Message msg);

    //更新接收状态
    @Update("update message set is_delivered=#{isDelivered} where message_id=#{messageId}")
    void updateDeliveryStatus(Long messageId, boolean isDelivered);


    //接收未读信息
    @Select("select * from message where is_delivered=FALSE and receiver_id=#{userId}")
    List<Message> getUnreadMessages(Long userId);

    //查询这两个用户之间的历史消息
    @Select("SELECT * FROM message" +
            "    WHERE (sender_id = #{senderId} AND receiver_id = #{receiverId})" +
            "       OR (sender_id = #{receiverId} AND receiver_id = #{senderId})" +
            "    ORDER BY create_time ASC")
    List<Message> getHistoryMessages(Long senderId, Long receiverId);

    // 获取对话用户id列表
    List<Long> getInterlocutorId(@Param("currentUserId") Long currentUserId);
}
